﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Shoping_cart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            if (Session["item_id"] == null)
            {
                string id = Request["id"];
                Session["item_id"] = id;
            }
       
            
            ItemDAO itDAO = new ItemDAO();
            item = itDAO.GetItem_by_ID(int.Parse(Session["item_id"].ToString()));
            Session["item_id_1"] = item.Item_id.ToString();
            Session["item_name"] = item.Name;
            Session["price"] = item.Price.ToString();
            Session["quantity"] = item.Quantity.ToString();
          
            load(item);
        }
        
    }

    Item item = new Item();
  
    
    private void load(Item item)
    {
            IList<Item> list = new List<Item>();
            list.Add(item);
            grv_shoping_cart.DataSource = list;
            grv_shoping_cart.DataBind();
    }
    private Item get_item(Item item)
    {
        item.Item_id = int.Parse(Session["item_id_1"].ToString());
        string ct_name1 = Session["item_name"].ToString();
        item.Name = ct_name1;

        item.Price = decimal.Parse(Session["price"].ToString());
        item.Quantity = int.Parse(Session["quantity"].ToString());
        return item;
    }

    protected void grv_shoping_cart_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grv_shoping_cart.EditIndex = e.NewEditIndex;        
        load(get_item(item));
    }
    protected void grv_shoping_cart_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grv_shoping_cart.EditIndex = -1;
        load(get_item(item));
    }
    protected void grv_shoping_cart_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void grv_shoping_cart_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        
        //item.Item_id  = int.Parse(grv_shoping_cart.DataKeys[e.RowIndex].Value.ToString());
        string ct_name = ((TextBox)grv_shoping_cart.Rows[e.RowIndex].FindControl("txtQuantity")).Text;
      //  Item item = new Item();
        item.Item_id = int.Parse(Session["item_id_1"].ToString());
        string ct_name1 = Session["item_name"].ToString();
        item.Name = ct_name1;

        item.Price = decimal.Parse(Session["price"].ToString());
        item.Quantity = int.Parse(ct_name);
        //if (item.Quantity == 2)
        //{
        //    Response.Redirect("Home.aspx");
        //}
        
            
            Session["item_id"] = item.Item_id.ToString();
            Session["item_name"] = item.Name.ToString();
            Session["price"] = item.Price.ToString();
            Session["quantity"] = item.Quantity.ToString();
            grv_shoping_cart.EditIndex = -1;
                load(item);
            
            
     //   item.Quantity = int.Parse(ct_name);
        //item.Name = ((Label)grv_shoping_cart.Rows[e.RowIndex].FindControl("Label2")).Text;
        //item.Price = decimal.Parse(((Label)grv_shoping_cart.Rows[e.RowIndex].FindControl("Label3")).Text.ToString());
     
    }
    public void onCheckedChange(object sender, EventArgs e)
    {
        if (((CheckBox)sender).Checked)
            foreach (GridViewRow row in grv_shoping_cart.Rows)
            {
                ((CheckBox)row.FindControl("chkChecked")).Checked = true;
            }
        else
        {
            foreach (GridViewRow row in grv_shoping_cart.Rows)
            {
                ((CheckBox)row.FindControl("chkChecked")).Checked = false;
            }
        }
    }
}